﻿using fmtx.Utility;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace website.Controllers
{
    public class ImgController : Controller
    {
        //
        // GET: /Img/

        /// <summary>
        /// 缓存输出缩略图
        /// </summary>
        /// <param name="file"></param>
        /// <param name="m"></param>
        /// <param name="w"></param>
        /// <param name="h"></param>
        /// <returns></returns>
        [OutputCache(Duration = 720 * 60 * 20)]
        public ActionResult Index(string file, ImageFactory.ImgModel m, int w = 0, int h = 0)
        {
            try
            {
                var res = StaticCs.MakeThumbnail(file, w, h, m);
                if (res.Width != 0 && res.Height != 0)
                {
                    return File(Server.MapPath(StaticCs.VirtualPath) + res.FileName, "image/" + Path.GetExtension(file).Replace(".", ""));
                }
            }
            catch (Exception ex)
            {
                fmtx.Framework.Log.Error(ex);
            }
            Response.CacheControl = "no-cache";
            Response.AddHeader("Pragma", "No-Cache");
            Response.Cache.SetNoStore();
            return File(ImageFactory.MakeThumbnail(Server.MapPath("~/css/images/default.png"), w, h, m), "image/png");
        }

        /// <summary>
        /// 缓存输出缩略图
        /// </summary>
        /// <param name="file"></param>
        /// <param name="m"></param>
        /// <param name="w"></param>
        /// <param name="h"></param>
        /// <returns></returns>
        [OutputCache(Duration = 720 * 60 * 20)]
        public ActionResult OnlyCache(string file, ImageFactory.ImgModel m, int w = 0, int h = 0)
        {
            try
            {
                var data = ImageFactory.MakeThumbnail(Server.MapPath(StaticCs.VirtualPath) + file, w, h, m);
                return File(data, "image/" + Path.GetExtension(file).Replace(".", ""));
            }
            catch (Exception ex)
            {
                fmtx.Framework.Log.Error(ex);
            }
            Response.CacheControl = "no-cache";
            Response.AddHeader("Pragma", "No-Cache");
            Response.Cache.SetNoStore();
            return File(ImageFactory.MakeThumbnail(Server.MapPath("~/css/images/default.png"), w, h, m), "image/png");
        }

        /// <summary>
        /// 无缓存输出缩略图
        /// </summary>
        /// <param name="file"></param>
        /// <param name="m"></param>
        /// <param name="w"></param>
        /// <param name="h"></param>
        /// <returns></returns>
        public ActionResult NoCache(string file, ImageFactory.ImgModel m, int w = 0, int h = 0)
        {
            try
            {
                var filepath = Server.MapPath(StaticCs.VirtualPath) + file;
                if (System.IO.File.Exists(filepath))
                {
                    return File(ImageFactory.MakeThumbnail(filepath, w, h, m), "image/" + Path.GetExtension(file).Replace(".", ""));
                }
            }
            catch (Exception ex)
            {
                fmtx.Framework.Log.Error(ex);
            }
            Response.CacheControl = "no-cache";
            Response.AddHeader("Pragma", "No-Cache");
            Response.Cache.SetNoStore();
            return File(ImageFactory.MakeThumbnail(Server.MapPath("~/css/images/default.png"), w, h, m), "image/png");
        }

        /// <summary>
        /// 无缓存输出临时缩略图
        /// </summary>
        /// <param name="file"></param>
        /// <param name="m"></param>
        /// <param name="w"></param>
        /// <param name="h"></param>
        /// <returns></returns>
        public ActionResult Temp(string file, ImageFactory.ImgModel m, int w = 0, int h = 0)
        {
            try
            {
                var data = ImageFactory.MakeThumbnail(Server.MapPath(StaticCs.VirtualPath) + file, w, h, m);
                return File(data, "image/" + Path.GetExtension(file).Replace(".", ""));
            }
            catch (Exception ex)
            {
                fmtx.Framework.Log.Error(ex);
            }
            Response.CacheControl = "no-cache";
            Response.AddHeader("Pragma", "No-Cache");
            Response.Cache.SetNoStore();
            return File(ImageFactory.MakeThumbnail(Server.MapPath("~/css/images/default.png"), w, h, m), "image/png");
        }

        /// <summary>
        /// 原图输出
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [OutputCache(Duration = 720 * 60 * 20)]
        public ActionResult Artwork(string file)
        {
            try
            {
                var filepath = Server.MapPath(StaticCs.VirtualPath) + file;
                if (System.IO.File.Exists(filepath))
                {
                    return File(filepath, "image/" + Path.GetExtension(file).Replace(".", ""));
                }
            }
            catch (Exception ex)
            {
                fmtx.Framework.Log.Error(ex);
            }
            Response.CacheControl = "no-cache";
            Response.AddHeader("Pragma", "No-Cache");
            Response.Cache.SetNoStore();
            return File(Server.MapPath("~/css/images/default.png"), "image/png");
        }

    }
}
